<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogVisible"
    width="50%"
    :before-close="handleClose"
  >
    <el-form ref="form" :model="form" label-width="120px">
      <el-form-item label="名称：">
        <el-input v-model="form.name"></el-input>
      </el-form-item>
      <el-form-item label="排序：">
        <el-input v-model="form.sort"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="handleClose">取 消</el-button>
      <el-button type="primary" :loading="loading" @click="onSubmit">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script lang="ts">
import Vue from 'vue'
import { updateResourceCategories } from '@/services/resource-category'

export default Vue.extend({
  name: 'CreateOrEditClass',
  data() {
    return {
      form: {},
      loading: false
    }
  },
  computed: {
    title: function () {
      if (this.isEdit) {
        return '编辑资源'
      }
      return '新建资源'
    }
  },
  mounted () {
    this.form = JSON.parse(JSON.stringify(this.formData)) || {}
  },
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    },
    isEdit: {
      type: Boolean,
      default: false
    },
    formData: {
      type: Object,
      default: function () {
        return {}
      }
    }
  },
  methods: {
    handleClose() {
      this.$emit('update:dialogVisible', false)
      console.log('submit!')
    },
    async onSubmit() {
      this.loading = true
      const res: any = await updateResourceCategories(this.form)
      this.loading = false
      this.$emit('reloadList')
      this.$emit('update:dialogVisible', false)
    }
  }
})
</script>

<style lang="scss" scoped></style>
